﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using System.Collections.Generic; 

public class EncryptDecryptQueryString
{
    private byte[] key = { };
    private byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };

    public string Decrypt(string stringToDecrypt, string sEncryptionKey = "r0b1nr0y")
    {
        byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            inputByteArray = Convert.FromBase64String(stringToDecrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
            return encoding.GetString(ms.ToArray());
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

    public string Encrypt(string stringToEncrypt, string SEncryptionKey = "r0b1nr0y")
    {
        try
        {
            key = System.Text.Encoding.UTF8.GetBytes(SEncryptionKey);
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Convert.ToBase64String(ms.ToArray());
        }
        catch (Exception e)
        {
            return e.Message;
        }
    }

    public List<PLMstFormMasters> Decrypt(List<PLMstFormMasters> p_lstQueryString, string stringToDecrypt, string sEncryptionKey = "r0b1nr0y")
    {
        byte[] inputByteArray = new byte[stringToDecrypt.Length + 1];
        //try
        //{
        //    key = System.Text.Encoding.UTF8.GetBytes(sEncryptionKey);
        //    DESCryptoServiceProvider des = new DESCryptoServiceProvider();
        //    inputByteArray = Convert.FromBase64String(stringToDecrypt);
        //    MemoryStream ms = new MemoryStream();
        //    CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
        //    cs.Write(inputByteArray, 0, inputByteArray.Length);
        //    cs.FlushFinalBlock();
        //    System.Text.Encoding encoding = System.Text.Encoding.UTF8;
        //    string encodedPhrase =  encoding.GetString(ms.ToArray());

        //    string[] strSplitQueryString = encodedPhrase.Split('&');
        //    if (strSplitQueryString.Length == p_lstQueryString.Count)
        //    {
        //        foreach (string strQueryString in strSplitQueryString)
        //        {
        //            string[] strKeyValue = strQueryString.Split('=');
        //            PLMstFormMasters objForm = p_lstQueryString.Where(f => f.Track == strKeyValue[0]).SingleOrDefault();
        //            GlobalKeys.QueryStringDataType objQueryStringType;
        //            objQueryStringType = objForm.DataType;
        //            switch (objQueryStringType)
        //            {
        //                case GlobalKeys.QueryStringDataType.STRING:
        //                    {
        //                        objForm.QueryStringValue = strKeyValue[1];
        //                        break;
        //                    }
        //                case GlobalKeys.QueryStringDataType.INT:
        //                    {
        //                        int intQueryStringValue = 0;
        //                        try
        //                        {
        //                            intQueryStringValue = int.Parse(strKeyValue[1]);
        //                            objForm.QueryStringValue = intQueryStringValue;
        //                        }
        //                        catch (Exception eString)
        //                        {
        //                            return null;
        //                        }
        //                        break;
        //                    }
        //            }
        //        }
        //    }
        //    else
        //    {
        //        return null;
        //    }
        //}
        //catch (Exception ex)
        //{
        //    FormsAuthentication.SignOut();
        //    FormsAuthentication.RedirectToLoginPage();
        //    HttpContext.Current.Response.End();
        //}
        return p_lstQueryString;
    }
}
